<template>
  <span>{{time}}</span>
</template>


<script>
export default {
  data() {
    return {
      time: '',
      flag: false
    }
  },
  mounted() {
    let time = setInterval(() => {
      if (this.flag == true) {
        clearInterval(time)
        return
      }
      this.timeDown()
    }, 500)
  },
  props: {
    Dtime:{
      type: String
    },
    endTime: {
      type: String
    }
  },
  methods: {
    timeDown() {
      const nowTime = new Date()
      let leftTime = parseInt((this.endTime - (nowTime.getTime()+ this.Dtime)) / 1000)
      let d = parseInt(leftTime / (24 * 60 * 60))
      let h = this.formate(parseInt((leftTime / (60 * 60)) % 24))
      let m = this.formate(parseInt((leftTime / 60) % 60))
      let s = this.formate(parseInt(leftTime % 60))
      if (leftTime < 0) {
        console.log(`${this.endTime} 当前时间${nowTime.getTime()+ this.Dtime}`)
        this.flag = true
        this.$emit('timeEnd')
        return false
      }
      this.time = `${m}:${s}` // 需要修改时间样式的话 ，比如需要什么30分钟倒计时，就只保留分和秒
    },
    formate(time) {
      if (time >= 10) {
        return time
      } else {
        return `0${time}`
      }
    }
  }
}
</script>

<style scoped>
</style>